import Vue from 'vue'
import VueRouter from 'vue-router'

const routerList = []
function importRoutes(routerArr) {
  /* 动态路由 */
  routerArr.keys().forEach(key => {
    routerList.push(routerArr(key).default)
  })
}
importRoutes(require.context('./route', true, /\.routes\.js/))


Vue.use(VueRouter)


const routes = [{
  path: '/',
  redirect: '/login',
},
...routerList,
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

router.beforeEach((to, from, next) => {
  /* 比如做登陆验证*/
  const isAuthenticated = window.sessionStorage.getItem('token')
  if (to.path !== '/login' && !isAuthenticated) next({ path: '/login' })
  else next()

  // document.title = to.matched[0].name

})
export default router